// 蜂窝蜜造平台生成代码，如手工更改，请添加到 .beeignore 忽略生成
package com.fowo.api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fowo.api.common.model.IdListPo;
import com.fowo.api.common.model.IdPo;
import com.fowo.api.common.model.OptionItem;
import com.fowo.api.common.model.R;
import com.fowo.api.common.util.ResponseUtils;
import com.fowo.api.entity.InventoryList;
import com.fowo.api.model.inventory.list.InventoryListGetInventoryParam;
import com.fowo.api.model.inventory.list.InventoryListGetInventoryResult;
import com.fowo.api.model.inventory.list.InventoryListItemVo;
import com.fowo.api.model.inventory.list.InventoryListItemVo;
import com.fowo.api.model.inventory.list.InventoryListSearchParamPo;
import com.fowo.api.model.inventory.list.InventoryListSearchParamPo;
import com.fowo.api.model.inventory.list.InventoryListVo;
import com.fowo.api.service.InventoryListService;
import com.fowo.api.user.model.JwtUserInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StreamUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "盘点单")
@RequestMapping("/api/inventory-list")
@RestController
public class InventoryListController {

  @Resource
  private InventoryListService service;

  @Operation(summary = "查询结果")
  @GetMapping
  public R<List<InventoryListItemVo>> search(
    InventoryListSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.search(search));
  }

  @Operation(summary = "快速查询选项(有缓存)")
  @GetMapping("options")
  public R<List<OptionItem<InventoryListItemVo>>> searchOptions(
    InventoryListSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.searchOptions(search));
  }

  @Operation(summary = "查询最后更新时间")
  @GetMapping("lastUpdateTime")
  public R<Date> searchLastUpdateTime(InventoryListSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.searchLastUpdateTime(search));
  }

  @Operation(summary = "获取分页查询结果")
  @GetMapping("page")
  public R<Page<InventoryListItemVo>> getPage(
    InventoryListSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.pageSearch(search));
  }

  @Operation(summary = "获取包括子表的查询结果")
  @GetMapping("page-with-child")
  public R<Page<?>> getPageWithChild(InventoryListSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.pageWithChildSearch(search));
  }

  @Operation(summary = "仅获取子表的查询结果")
  @GetMapping("page-only-child/{parentId}")
  public R<Page<?>> getPageOnlyChild(
    @PathVariable Long parentId,
    InventoryListSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.pageOnlyChildSearch(parentId, search));
  }

  @Operation(summary = "查询是否后台下载")
  @GetMapping("getBackgroundExportFlag")
  public R<Boolean> getBackgroundExportFlag(InventoryListSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.getBackgroundExportFlag(search));
  }

  @Operation(summary = "获取详情")
  @GetMapping("{id}")
  public R<InventoryListVo> getVoById(@PathVariable Long id) {
    return R.data(() -> service.getVoById(id));
  }

  @Operation(summary = "获取盘点仓库相关的编号列表")
  @GetMapping("warehouse-stock/{texts}/ids")
  public R<List<Long>> getIdsByWarehouseStock(@PathVariable String texts) {
    List<Long> nameList = Arrays
      .stream(texts.split(","))
      .map(Long::parseLong)
      .collect(Collectors.toList());
    return R.data(() -> service.getIdsByWarehouseStock(nameList));
  }

  @Operation(summary = "获取编号相关的盘点仓库列表")
  @GetMapping("warehouse-stock/{ids}")
  public R<List<Long>> getVoById(@PathVariable String ids) {
    List<Long> idList = Arrays
      .asList(ids.split(","))
      .stream()
      .map(Long::valueOf)
      .collect(Collectors.toList());
    return R.data(() -> service.getWarehouseStockByIds(idList));
  }

  @PreAuthorize("@am.canAccess('inventoryListCreate')")
  @Operation(summary = "新增")
  @PostMapping
  public R<Long> create(@Validated @RequestBody InventoryListVo model) {
    return R.data(() -> service.create(model));
  }

  @PreAuthorize("@am.canAccess('inventoryListEdit')")
  @Operation(summary = "更新")
  @PutMapping
  public R update(@RequestBody InventoryListVo model) {
    return R.run(() -> service.update(model));
  }

  @PreAuthorize("@am.canAccess('inventoryListEdit')")
  @Operation(summary = "编辑更新")
  @PutMapping("edit")
  public R updateForEdit(@RequestBody InventoryListVo model) {
    return R.run(() -> service.updateForEdit(model));
  }

  @PreAuthorize("@am.canAccess('inventoryListDelete')")
  @Operation(summary = "删除")
  @DeleteMapping("{id}")
  public R delete(@PathVariable Long id) {
    return R.run(() -> service.delete(id));
  }

  @PreAuthorize("@am.canAccess('inventoryListDelete')")
  @Operation(summary = "批量删除")
  @DeleteMapping("batch")
  public R batchDelete(@RequestBody IdListPo po) {
    return R.run(() -> service.batchDelete(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('inventoryListCancellation')")
  @Operation(summary = "作废")
  @PostMapping("cancellation")
  public R cancellation(@RequestBody IdPo po) {
    return R.run(() -> service.cancellation(po.getId()));
  }

  @PreAuthorize("@am.canAccess('inventoryListSubmit')")
  @Operation(summary = "提交")
  @PostMapping("submit")
  public R submit(@RequestBody IdPo po) {
    return R.run(() -> service.submit(po.getId()));
  }

  @PreAuthorize("@am.canAccess('inventoryListBeginInventory')")
  @Operation(summary = "开始盘点")
  @PostMapping("beginInventory")
  public R beginInventory(@RequestBody IdPo po) {
    return R.run(() -> service.beginInventory(po.getId()));
  }

  @PreAuthorize("@am.canAccess('inventoryListLockInventory')")
  @Operation(summary = "锁库存")
  @PostMapping("lockInventory")
  public R lockInventory(@RequestBody IdPo idPo) {
    return R.run(() -> service.lockInventory(idPo.getId()));
  }

  @PreAuthorize("@am.canAccess('inventoryListUnLockInventory')")
  @Operation(summary = "释放库存")
  @PostMapping("unLockInventory")
  public R unLockInventory(@RequestBody IdPo idPo) {
    return R.run(() -> service.unLockInventory(idPo.getId()));
  }

  @PreAuthorize("@am.canAccess('inventoryListLockWarehouse')")
  @Operation(summary = "锁库存_仓库")
  @PostMapping("lockWarehouse")
  public R lockWarehouse(@RequestBody IdPo idPo) {
    return R.run(() -> service.lockWarehouse(idPo.getId()));
  }

  @PreAuthorize("@am.canAccess('inventoryListUnLockWarehouse')")
  @Operation(summary = "释放库存_仓库")
  @PostMapping("unLockWarehouse")
  public R unLockWarehouse(@RequestBody IdPo idPo) {
    return R.run(() -> service.unLockWarehouse(idPo.getId()));
  }

  @PreAuthorize("@am.canAccess('inventoryListDeleteInventory')")
  @Operation(summary = "删除")
  @PostMapping("deleteInventory")
  public R deleteInventory() {
    return R.run(() -> service.deleteInventory());
  }

  @PreAuthorize("@am.canAccess('inventoryListVoidInventory')")
  @Operation(summary = "作废")
  @PostMapping("voidInventory")
  public R voidInventory(@RequestBody IdListPo po) {
    return R.run(() -> service.voidInventory(po.getIds()));
  }

  @Operation(summary = "全部")
  @GetMapping("all-inventory")
  public R<List<InventoryListItemVo>> allInventory(
    InventoryListSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.allInventory(search));
  }

  @Operation(summary = "全部分页")
  @GetMapping("all-inventoryPage")
  public R<Page<InventoryListItemVo>> allInventoryPage(
    InventoryListSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.allInventoryPage(search));
  }

  @Operation(summary = "全部最后更新时间")
  @GetMapping("all-inventoryLastUpdateTime")
  public R<Date> allInventoryLastUpdateTime(InventoryListSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.allInventoryLastUpdateTime(search));
  }

  @Operation(summary = "待盘点")
  @GetMapping("wait-inventory")
  public R<List<InventoryListItemVo>> waitInventory(
    InventoryListSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.waitInventory(search));
  }

  @Operation(summary = "待盘点分页")
  @GetMapping("wait-inventoryPage")
  public R<Page<InventoryListItemVo>> waitInventoryPage(
    InventoryListSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.waitInventoryPage(search));
  }

  @Operation(summary = "待盘点最后更新时间")
  @GetMapping("wait-inventoryLastUpdateTime")
  public R<Date> waitInventoryLastUpdateTime(
    InventoryListSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.waitInventoryLastUpdateTime(search));
  }

  @Operation(summary = "盘点中")
  @GetMapping("inventorying")
  public R<List<InventoryListItemVo>> inventorying(
    InventoryListSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.inventorying(search));
  }

  @Operation(summary = "盘点中分页")
  @GetMapping("inventoryingPage")
  public R<Page<InventoryListItemVo>> inventoryingPage(
    InventoryListSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.inventoryingPage(search));
  }

  @Operation(summary = "盘点中最后更新时间")
  @GetMapping("inventoryingLastUpdateTime")
  public R<Date> inventoryingLastUpdateTime(InventoryListSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.inventoryingLastUpdateTime(search));
  }

  @Operation(summary = "已盘点")
  @GetMapping("inventoryed")
  public R<List<InventoryListItemVo>> inventoryed(
    InventoryListSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.inventoryed(search));
  }

  @Operation(summary = "已盘点分页")
  @GetMapping("inventoryedPage")
  public R<Page<InventoryListItemVo>> inventoryedPage(
    InventoryListSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.inventoryedPage(search));
  }

  @Operation(summary = "已盘点最后更新时间")
  @GetMapping("inventoryedLastUpdateTime")
  public R<Date> inventoryedLastUpdateTime(InventoryListSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.inventoryedLastUpdateTime(search));
  }

  @Operation(summary = "已作废")
  @GetMapping("inventory-abrogate")
  public R<List<InventoryListItemVo>> inventoryAbrogate(
    InventoryListSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.inventoryAbrogate(search));
  }

  @Operation(summary = "已作废分页")
  @GetMapping("inventory-abrogatePage")
  public R<Page<InventoryListItemVo>> inventoryAbrogatePage(
    InventoryListSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.inventoryAbrogatePage(search));
  }

  @Operation(summary = "已作废最后更新时间")
  @GetMapping("inventory-abrogateLastUpdateTime")
  public R<Date> inventoryAbrogateLastUpdateTime(
    InventoryListSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.inventoryAbrogateLastUpdateTime(search));
  }

  @Operation(summary = "生成盘点明细")
  @GetMapping("getInventoryInfo")
  public R<InventoryListGetInventoryResult> getInventory(
    InventoryListGetInventoryParam param
  ) {
    return R.data(() -> service.getInventory(param));
  }
}
